/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package formularios;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import modelo.ModeloTabela;
import modelo.Modelo_empregados;
import utilizador_bd.ConectBD;

/**
 *
 * @author Daniel
 */
public class Iva extends javax.swing.JFrame {
      ConectBD conectiva = new ConectBD();

    /**
     * Creates new form Iva
     */
    public Iva() {
        initComponents();
        conectiva.conexao();
        
      preencherTabela("select * from iva order by cod_iva");

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1cod_iva = new javax.swing.JLabel();
        jLabel2descricao = new javax.swing.JLabel();
        jLabel3iva = new javax.swing.JLabel();
        jTextField1cod_iva = new javax.swing.JTextField();
        jTextField2descricao = new javax.swing.JTextField();
        jTextField3iva = new javax.swing.JTextField();
        jButton1novo = new javax.swing.JButton();
        jButton2guardar = new javax.swing.JButton();
        jButton3eliminar = new javax.swing.JButton();
        jButton4edit = new javax.swing.JButton();
        jButton5logout = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1primeiro = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1cod_iva.setText("cod_iva");

        jLabel2descricao.setText("Descrição");

        jLabel3iva.setText("Iva:");

        jTextField1cod_iva.setEnabled(false);

        jTextField3iva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ivaActionPerformed(evt);
            }
        });

        jButton1novo.setText("novo");
        jButton1novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1novoActionPerformed(evt);
            }
        });

        jButton2guardar.setText("guardar");
        jButton2guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2guardarActionPerformed(evt);
            }
        });

        jButton3eliminar.setText("eliminar");
        jButton3eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3eliminarActionPerformed(evt);
            }
        });

        jButton4edit.setText("editar");

        jButton5logout.setText("logOut");
        jButton5logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5logoutActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jLabel1.setText("Produto->IVA");

        jButton1primeiro.setText("primeiro");
        jButton1primeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1primeiroActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(28, 28, 28)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1cod_iva)
                                        .addComponent(jLabel2descricao))
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGap(42, 42, 42)
                                            .addComponent(jTextField2descricao, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGap(18, 18, 18)
                                            .addComponent(jTextField1cod_iva, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGap(25, 25, 25)
                                    .addComponent(jLabel3iva, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(41, 41, 41)
                                    .addComponent(jTextField3iva, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 506, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(21, 21, 21)
                                    .addComponent(jButton1primeiro)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton1))))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(37, 37, 37)
                            .addComponent(jButton1novo, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(26, 26, 26)
                            .addComponent(jButton2guardar)
                            .addGap(39, 39, 39)
                            .addComponent(jButton3eliminar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton4edit)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton5logout)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(251, 251, 251)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1cod_iva, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1cod_iva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2descricao)
                        .addComponent(jLabel3iva)
                        .addComponent(jTextField3iva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextField2descricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(72, 72, 72)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1novo)
                    .addComponent(jButton2guardar)
                    .addComponent(jButton3eliminar)
                    .addComponent(jButton4edit)
                    .addComponent(jButton5logout))
                .addGap(49, 49, 49)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1primeiro)
                    .addComponent(jButton1))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(89, 89, 89))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField3ivaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ivaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ivaActionPerformed

    private void jButton2guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2guardarActionPerformed
       try {
               PreparedStatement pst;
    
          pst = conectiva.con.prepareStatement("insert into iva (descricao,ivaprod)values (?,?)");
                   
            pst.setString(1, jTextField2descricao.getText());
            pst.setString(2, jTextField3iva.getText());
          
          pst.executeUpdate();
            JOptionPane.showMessageDialog(rootPane, "Salvo com sucesso!");

          
          } catch (SQLException ex) {
JOptionPane.showMessageDialog(rootPane, "Erro na inserção!\n Erro:" +ex);          }
            jTextField1cod_iva.setEnabled(false);
            jTextField2descricao.setEnabled(false);
            jTextField3iva.setEnabled(false);
            jButton2guardar.setEnabled(false);
            jButton3eliminar.setEnabled(false);
            jButton4edit.setEnabled(false);
            jButton1novo.setEnabled(true);
            
  

          
    }//GEN-LAST:event_jButton2guardarActionPerformed

    private void jButton1novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1novoActionPerformed
  jTextField1cod_iva.setEnabled(false);
            jTextField2descricao.setEnabled(true);
            jTextField3iva.setEnabled(true);
            jButton2guardar.setEnabled(true);
            jButton3eliminar.setEnabled(false);
            jButton4edit.setEnabled(false);
            jButton1novo.setEnabled(false);    }//GEN-LAST:event_jButton1novoActionPerformed

    private void jButton5logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5logoutActionPerformed
int resposta ;
        
        resposta=JOptionPane.showConfirmDialog(null, "Quer mesmo sair do programa?");       

       if (resposta == JOptionPane.YES_OPTION) {

dispose();



} else {


}     }//GEN-LAST:event_jButton5logoutActionPerformed

    public void preencherTabela(String SQL){
        ArrayList dados = new ArrayList();
        
        String [] Colunas = new String[]{"Cod_Iva", "Designação", "ValorIva"};
        
        conectiva.executaSQL(SQL);
          try {
              conectiva.re.first();
              
                do{
         dados.add(new Object[]{conectiva.re.getInt("cod_iva"),conectiva.re.getString("descricao"),conectiva.re.getInt("ivaprod")});
     } while(conectiva.re.next());
     
          } catch (SQLException ex) {
           JOptionPane.showMessageDialog(null, "Erro no array!\n Erro:" +ex.getMessage());
          }
          
     ModeloTabela modelo = new ModeloTabela(dados, Colunas);
     
     jTable2.setModel(modelo);
     jTable2.getColumnModel().getColumn(0).setPreferredWidth(110);
     jTable2.getColumnModel().getColumn(0).setResizable(false);
     jTable2.getColumnModel().getColumn(1).setResizable(false);
     jTable2.getColumnModel().getColumn(1).setPreferredWidth(110);
     jTable2.getColumnModel().getColumn(2).setResizable(false);
     jTable2.getColumnModel().getColumn(2).setPreferredWidth(110);


     jTable2.getTableHeader().setReorderingAllowed(false);
     jTable2.setAutoResizeMode(jTable2.AUTO_RESIZE_OFF);
     jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
    }
    
    private void jButton1primeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1primeiroActionPerformed
conectiva.executaSQL("select *  from iva");
          try {
              conectiva.re.first();
              
              jTextField1cod_iva.setText(String.valueOf(conectiva.re.getInt("cod_iva")));
              jTextField2descricao.setText(conectiva.re.getString("descricao"));
              jTextField3iva.setText(String.valueOf(conectiva.re.getInt("ivaprod")));
              

          } catch (SQLException ex) {
           JOptionPane.showMessageDialog(rootPane, "Erro no anterior!\n Erro:" +ex.getMessage());
          }
    }//GEN-LAST:event_jButton1primeiroActionPerformed

    private void jButton3eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3eliminarActionPerformed
 try {
            PreparedStatement pft;
            pft = conectiva.con.prepareStatement("delete from iva where cod_iva='" +jTextField1cod_iva.getText()+"'");
            pft.executeUpdate();
            
            JOptionPane.showMessageDialog(rootPane, "Eliminado com sucesso:");
        } catch (SQLException ex) {
           JOptionPane.showMessageDialog(rootPane, "Erro na inserção!\n Erro:" +ex.getMessage());
        }
        
        
        
            jTextField1cod_iva.setEnabled(false);
            jTextField2descricao.setEnabled(false);
            jTextField3iva.setEnabled(false);
        

            jButton2guardar.setEnabled(false);
            jButton3eliminar.setEnabled(false);
            jButton4edit.setEnabled(false);
            jButton1novo.setEnabled(true);        }//GEN-LAST:event_jButton3eliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Iva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Iva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Iva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Iva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Iva().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton1novo;
    private javax.swing.JButton jButton1primeiro;
    private javax.swing.JButton jButton2guardar;
    private javax.swing.JButton jButton3eliminar;
    private javax.swing.JButton jButton4edit;
    private javax.swing.JButton jButton5logout;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel1cod_iva;
    private javax.swing.JLabel jLabel2descricao;
    private javax.swing.JLabel jLabel3iva;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1cod_iva;
    private javax.swing.JTextField jTextField2descricao;
    private javax.swing.JTextField jTextField3iva;
    // End of variables declaration//GEN-END:variables
}
